<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('goods_class', LIB_PATH);
$main = new Goods($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;
$cate_pid_list = $main->get_cate_array("cate_pid = '0' ",'cate_id DESC','','cate_id,cate_name');
if($cate_pid_list){
	foreach($cate_pid_list as $k=>$v){
		$cate_pid_ary[$v['cate_id']] = $v['cate_name']; 
	} 
}
$pid = intval($_GET['pid']);

switch ($cf_action) {
    case "index":
	$smarty->mypage($cf_controller, $cf_action, 0);	
	$data_ary = $main->load_cate_data();
	
/*	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);	
	$where = '1';
	if ($skey) {
	    $where .= " and cate_name like '%" . $skey . "%'";
	} 	
	
	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name2(), $where, " cate_sort_num ASC,cate_id DESC", intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt . '&skey=' . $skey);*/
	
	//echo $MyPage->get_sql();
	//dump($data_ary);
	break;
    case "add":
	if ($cf_ispost) {
		$data = $db->getFormData($main->get_table_name2(),$_POST);
		$data['cate_create_time'] = date("Y-m-d H:i:s");		
	    $result = $main->insert_data_cate($data);		
	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page.'?dpt='.$_POST['dpt'];	
		unset($_SESSION['pre_url']);
		$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
		my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
		my_jump('添加失败');
	    }
	} else {
		 $smarty->mypage($cf_controller, $cf_action, 0);
		 $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
       /* $goods_list = $main->get_goods();
        foreach($goods_list as $v){
            $goods_arr[$v['goods_id']] = $v['goods_title'];
        }
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
	    //dump($_REQUEST);*/
	}

	break;
    case "edit":
	if ($cf_ispost) {
	    $data = $db->getFormData($main->get_table_name2(),$_POST);
		$data['cate_edit_time'] = date("Y-m-d H:i:s");	   
	    $result = $main->update_data_cate($data, "cate_id='$id'");

	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		    my_success('修改成功', '', $return_page, 20);
	    } else {
		    my_error('修改失败');
	    }
	} else {      
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		$row = $main->get_cate_row($id);
	    if (!$row)
		my_jump('沒有此信息');		
	   
	}
	break;	
    case "del":
	$result = $main->del_data_cate($id);
	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失敗!');
	}

	break;
    case "searchtitle":

        $key = strtolower($_GET["q"]);
        $list = $main->search_title($key);
        if($list){
            foreach($list as $k=>$v){
                echo $v['goods_title'].'|'.$v['goods_id']."\n";
            }
        }
        exit;

        break;
    default:
	my_jump('非法操作!');
	break;
}

$smarty->assign(array(
    'id' => $id,
    'row' => htmlchar($row),
    'data_ary' => $data_ary,
    'sessionid' => session_id(),
    'page' => $page,
    'self' => $self,
    'skey' => $skey,
    'skey2' => $skey2,
    'skey3' => $skey3,
    'skey4' => $skey4,
    'skey5' => $skey5,
    'stype' => $stype,
    'pid' => $pid ,
    'datas1' => $datas1,
    'group' => $group,
    'result' => $result,
    'sum' => $sum,
    'goods_arr' => add_array_item(array('' => '请选择商品'), $goods_arr),
    'type' => add_array_item(array('' => '全部'), $cf_info_var_common['goods_cate']),
	'cate_pid_ary' => add_array_item(array('' => '无'), $cate_pid_ary)
	
));

$smarty->mydisplay();
?>